const wx2my = require('../../../wx2my');
const Behavior = require('../../../Behavior');
var base = require('./../../../config.js');
var app = getApp();

var QQMapWX = require('./qqmap-wx-jssdk.js');

var qqmapsdk;
Page({
  data: {
    hotel: [],
    address: '',
    searchAddress: '',
    toView: 'inToView0'
  },
  scrollToViewFn: function (e) {
    var _id = e.currentTarget.dataset.id;
    console.log(e);
    this.setData({
      toView: 'inToView' + _id
    });
    console.log(this.data.toView);
  },

  // 计算两地距离
  distance: function (la1, lo1, la2, lo2) {    
    var La1 = la1 * Math.PI / 180.0;   
    var La2 = la2 * Math.PI / 180.0;  
    var La3 = La1 - La2;   
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;   
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));  
        s = s * 6378.137;//地球半径    
        s = Math.round(s * 10000) / 10000;   
        console.log("计算结果", s)   
         return s  
  },

  onLoad: function (o) {
   console.log('距离'+this.distance(30.2,120.1,30.5,120))
    var that = this;
    that.setData({
      searchAddress: o.address
    });
    wx2my.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3090F4'
    });

    wx2my.newRequest({
      url:base.siteroot,
      data: {
        c:'entry',
        a:'wxapp',
        do:'JdList',
        m:'zh_jdgjb',
        keywords:o.address
      },
      success: (o) => {
        console.log(o);
        that.setData({
          hotel: o.data
        });
      },
      fail: (err) => {
         that.setData({
          hotel: []
        });
      }
    })


    // app.util.request({
    //   url: "entry/wxapp/JdList",
    //   cachetime: "0",
    //   data: {
    //     keywords: o.address
    //   },
    //   success: function (o) {
    //     console.log(o);
    //     that.setData({
    //       hotel: o.data
    //     });
    //   },
    //   fail: function () {
    //     that.setData({
    //       hotel: []
    //     });
    //   }
    // }); // 实例化API核心类

    // qqmapsdk = new QQMapWX({
    //   key: 'DXJBZ-ZQEKW-UOPRE-RAZT3-CI6NK-WOBF5'
    // }); //1、获取当前位置坐标

    // // ??????????????????????????????????????????????????
    // wx2my.getLocation({
    //   type: 'wgs84',
    //   success: function (res) {
    //     console.log(res); //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析

    //     qqmapsdk.reverseGeocoder({
    //       location: {
    //         latitude: res.latitude,
    //         longitude: res.longitude
    //       },
    //       success: function (addressRes) {
    //         var address = addressRes.result.formatted_addresses.recommend;
    //         that.setData({
    //           address: address,
    //           latitude: res.latitude,
    //           longitude: res.longitude
    //         });
    //       },
    //       fail: function (e) {
    //         console.log(e);
    //       },
    //       complete: function (e) {
    //         console.log(e);
    //       }
    //     });
    //   }
    // });


  },
  search_hotel: function (o) {
    console.log(o);
    var t = this,
        e = o.detail.value;
    "" != e ? wx2my.newRequest({
      url:base.siteroot,
      data: {
        c:'entry',
        a:'wxapp',
        do:'JdList',
        m:'zh_jdgjb',
        keywords:o.address
      },
      success: (o) => {
        console.log(o)
        t.setData({
          hotel: o.data
        });
      }
    }) : t.setData({
      hotel: []
    });

    // app.util.request({
    //   url: "entry/wxapp/JdList",
    //   cachetime: "0",
    //   data: {
    //     keywords: e
    //   },
    //   success: function (o) {
    //     console.log(o)
    //     t.setData({
    //       hotel: o.data
    //     });
    //   }
    // })

  },
  
  local: function () {
    // var a = this;
    // var t = a.data;
    // console.log('search.js中的'+ t.latitude)
    // console.log(t.latitude)
    // wx2my.setStorageSync("latitude", t.latitude)
    // wx2my.setStorageSync("longitude", t.longitude)

    wx2my.navigateTo({
      url: "../hotel_list/hotel_list?nearby=0"
    });
  },

  search_result: function (o) {
    var t = o.currentTarget.dataset.id;
    wx2my.navigateTo({
      url: "../hotel_list/hotel_info?hotel_id=" + t
    });
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {
    this.setData({
      hotel: []
    });
  },
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {}
});